|
|
|
|
|
|
|
This example is relatively simple because it sets each byte in the array to 0. Doing anything more complex is much harder. The main difficulty lies in mapping the bytes in the array to the colors of the pixels in the image. Depending on how your computer is configured, bitmaps on your system may use 1, 4, 8, 15, 16, 24, 32, or some other number of bits per pixel. These different color depths lead to different arrangements of bits in the array. |
|
|
|
|
|
|
|
|
As if this were not confusing enough, Windows handles different color depths in fundamentally different ways. If an image uses eight or fewer bits per pixel, the bits correspond to indexes in a color table. If an image uses more than eight bits per pixel, the bits give the red, green, and blue components of the pixel's color directly. |
|
|
|
|
|
|
|
|
Each pixel in a 15-bit image corresponds to two bytes in the array. The least significant five bits contain the pixel's blue component. The next five bits contain the pixel's green component. The last five bits contain the pixel's red component. The final bit is unused. |
|
|
|
|
|
|
|
|
Images that use 16-bit color are similar to 15-bit images except they use six bits to represent the pixels' green components instead of just five. Unfortunately, the bmBitsPixel field in the BITMAP data structure filled in by the GetObject API function contains the value 16 for both 15- and 16-bit images. That means you cannot easily tell whether an image uses 15- or 16-bit color. |
|
|
|
|
|
|
|
|
One way to solve this problem is to draw a white pixel on the image using PSet. Then use GetBitmapBits to read the pixel's bit value and see if the leftmost of the 16 bits is set. If it is, the color is using all 16 bits, and this is a 16-bit image. If the leftmost bit is not set, this is a 15-bit image. |
|
|
|
|
|
|
|
|
For 24-bit images, each pixel corresponds to three bytes that contain the pixel's red, green, and blue color components. The one-to-one mapping of bytes to color components makes 24-bit images relatively easy to manipulate. |
|
|
|
|
|
|
|
|
The 32-bit systems I have encountered assign four bytes for each pixel, but they actually use only three. The first three bytes give the pixel's color components, and the fourth byte is ignored. Using four bytes per pixel aligns each pixel's bytes on a four-byte boundary, and that simplifies the hardware. |
|
|
|
|
|
|
|
|
By now you have probably decided that working with all of these different color depths is extremely confusing. To make things easier, you can store pixel information using the RGBTriplet data type shown in the following code. Then you can manage the pixels' red, green, and blue color components separately and intuitively no matter what color model you use. |
|
|
|
 |
|
|
|
|
Public Type RGBTriplet
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
End Type |
|
|
|
|
|
|
|
|
Now you can write routines to transfer pixel values between an array of RGBTriplets and a bitmap. For example, the following code copies the bits representing a 15-bit image's pixel data from the array bytes into the array of RGBTriplets names pixels. |
|
|
|
|
|